package com.saihai.jiankang.Jkleave.controller;

import com.saihai.common.annotation.Log;
import com.saihai.common.config.RuoYiConfig;
import com.saihai.common.constant.JianKangConstant;
import com.saihai.common.core.controller.BaseController;
import com.saihai.common.core.domain.AjaxResult;
import com.saihai.common.core.domain.model.LoginUser;
import com.saihai.common.core.page.TableDataInfo;
import com.saihai.common.enums.BusinessType;
import com.saihai.common.exception.ServiceException;
import com.saihai.common.utils.MessageUtils;
import com.saihai.common.utils.StringUtils;
import com.saihai.common.utils.file.FileUploadUtils;
import com.saihai.common.utils.file.MimeTypeUtils;
import com.saihai.common.utils.poi.ExcelUtil;
import com.saihai.jiankang.Jkleave.domain.JkLeave;
import com.saihai.jiankang.Jkleave.service.IJkLeaveService;
import com.saihai.jiankang.student.domain.UfXs;
import com.saihai.jiankang.student.service.IUfXsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 请假Controller
 * 
 * @author ruoyi
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/jiankang/Jkleave")
public class JkLeaveController extends BaseController
{
    @Autowired
    private IJkLeaveService jkLeaveService;

    @Autowired
    private IUfXsService iUfXsService;

    /**
     * 查询请假列表
     */
    @GetMapping("/list")
    public TableDataInfo list(JkLeave jkLeave)
    {
        if (StringUtils.isNull(jkLeave.getXsId())) {
            throw new ServiceException(MessageUtils.message("学生id不能为空！"));
        }
        List<JkLeave> list = jkLeaveService.selectJkLeaveList(jkLeave);
        return getDataTable(list);
    }

    /**
     * 查询请假列表
     */
    @GetMapping("/leaveListBzr")
    public TableDataInfo leaveListBzr(JkLeave jkLeave)
    {
        if (StringUtils.isNull(jkLeave.getBjId())) {
            throw new ServiceException(MessageUtils.message("班级不能为空！"));
        }
        List<JkLeave> list = jkLeaveService.selectJkLeaveList(jkLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假列表
     */
    @Log(title = "导出请假列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JkLeave jkLeave)
    {
        List<JkLeave> list = jkLeaveService.selectJkLeaveList(jkLeave);
        ExcelUtil<JkLeave> util = new ExcelUtil<JkLeave>(JkLeave.class);
        util.exportExcel(response, list, "请假数据");
    }

    /**
     * 获取请假详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jkLeaveService.selectJkLeaveById(id));
    }

    /**
     * 新增请假
     */
    @Log(title = "新增请假", businessType = BusinessType.INSERT)
    @PostMapping("/addLeave")
    public AjaxResult add(@RequestBody JkLeave jkLeave)
    {
        jkLeave.setStatus(JianKangConstant.SHZ);
        //查询学生信息
        UfXs ufXs = iUfXsService.selectUfXsById(jkLeave.getXsId());
        if (StringUtils.isNotNull(ufXs)) {
            jkLeave.setName(ufXs.getXm());
            jkLeave.setBjId(Long.parseLong(ufXs.getSzbjId()));
            jkLeave.setXxId(Long.parseLong(ufXs.getXxId()));
        }
        return toAjax(jkLeaveService.insertJkLeave(jkLeave));
    }

    /**
     * 头像上传
     */
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            return AjaxResult.success("成功",avatar);
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 修改请假
     */
    @Log(title = "修改请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JkLeave jkLeave)
    {
        return toAjax(jkLeaveService.updateJkLeave(jkLeave));
    }

    /**
     * 删除请假
     */
    @Log(title = "删除请假", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jkLeaveService.deleteJkLeaveByIds(ids));
    }
}
